package com.abigdreamer.im.client.module.main.headtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Box;
import javax.swing.JPanel;

import com.abigdreamer.im.client.module.main.EIMMainFrame;
import com.abigdreamer.im.client.module.talk.TalkWindow;
import com.abigdreamer.im.client.module.vo.MsgUser;
import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.animation.Invoker;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCLabel;

import craky.layout.LineLayout;

public class EIMTreeNode extends JPanel implements MouseListener {

	private static final long serialVersionUID = 7505504403387251315L;

	private MsgUser user;
	private UserPopoInfo popoInfo;

	public EIMTreeNode(MsgUser user) {
		this.addMouseListener(this);
		this.user = user;
		priantNode(user);
	}

	public void priantNode(MsgUser user) {

//		Box box = Box.createHorizontalBox();
		EmptyComponent box = new EmptyComponent();
//		box.setAlignmentX(Box.LEFT_ALIGNMENT);
		box.setLayout(new LineLayout(10, 8, 0, 3, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
//		box.setAlignmentY(Box.CENTER_ALIGNMENT);
		
		String headImage = (String) user.getAttribute("headImage");
		NodeIcon nodeIcon = new NodeIcon(headImage, user.isOnline());
		box.add(nodeIcon);

		Box verticalBox = Box.createVerticalBox();
		verticalBox.setAlignmentX(Box.LEFT_ALIGNMENT);
		verticalBox.setAlignmentY(Box.CENTER_ALIGNMENT);
		
		JCLabel personWordLale = new JCLabel(user.getUserName());
		JCLabel kongjianLale = new JCLabel(user.getPersonWord());
		verticalBox.add(personWordLale);
		verticalBox.add(kongjianLale);
		verticalBox.add(new JCLabel("..."));
		box.add(verticalBox);
		
		this.setLayout(new BorderLayout());
		this.add(box, BorderLayout.WEST);
		
		this.setBackground(Color.white);
		this.setVisible(false);
		
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println(user.getUserName() + "Clicked");
	}

	public void mouseEntered(MouseEvent e) {
		setBackground(new Color(193, 225, 248));
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		popoInfo = new UserPopoInfo(this.user);
		EIMMainFrame v = EIMMainFrame.getInstance();
		popoInfo.setLocation(v.getX() - popoInfo.getWidth(), this.getY() + 170);
		Invoker.show(popoInfo);
	}

	public void mouseExited(MouseEvent e) {
		setBackground(Color.white);
		setCursor(Cursor.getDefaultCursor());
		popoInfo.setVisible(false);
	}

	public void mousePressed(MouseEvent e) {
		if (e.getClickCount() == 2) {
			TalkWindow talkFrame = new TalkWindow(user);
			talkFrame.setVisible(true);
			SwingUtil.runApp(talkFrame);
		}
	}

	public void mouseReleased(MouseEvent e) {
	}

	public MsgUser getUser() {
		return user;
	}

	public void setUser(MsgUser user) {
		this.user = user;
	}

}
